package com.shihang.crm.bak;

import java.awt.Point;
import java.io.File;

public class AndroidUtil {
	//CmdUtil.exec("mspaint E:\\pic\\screenshot.png");

	public static Point getAndroidScreen() {
		Point point = new Point();
		String content = CmdUtil.exec("adb shell dumpsys window displays");
		System.out.println(content);
		String[] lines = content.split("\n");
		for (String line : lines) {
			String[] strs = line.split(" ");
			for (String str : strs) {
				if (str.startsWith("cur=")) {
					str = str.replace("cur=", "");
					String[] sizes = str.split("x");
					point.x = Integer.parseInt(sizes[0]);
					point.y = Integer.parseInt(sizes[1]);
					return point;

				}

			}
		}
		return point;
	}

	public static void click(int x, int y) {
		CmdUtil.exec(String.format("adb shell input tap %s %s", x, y));
	}

	public static void click(Point point) {
		click(point.x, point.y);
	}

	public static void swipe(int fromX, int fromY, int toX, int toY,
			int duration) {
		CmdUtil.exec(String.format("adb shell input swipe %s %s %s %s %s",
				fromX, fromY, toX, toY, duration));
	}

	public static void swipe(int fromX, int fromY, int toX, int toY) {
		CmdUtil.exec(String.format("adb shell input swipe %s %s %s %s", fromX,
				fromY, toX, toY));
	}

	public static void input(String content) {
		CmdUtil.exec(String.format("adb shell input text \"%s\"", content));
	}

	public static void back() {
		click(1900, 50);
	}
	
	
	public static void openDingTalk(){
		CmdUtil.exec("adb shell input keyevent 82");//解锁手机
		sleep(500);
		CmdUtil.exec("adb shell input keyevent 3");//回到home
		sleep(500);
		CmdUtil.exec("adb shell am start -n com.alibaba.android.rimet/com.alibaba.android.rimet.biz.SplashActivity");//打开钉钉
	}
	
	public static void screen(){
		screen("");
	}
	
	public static void screen(String path){
		if(null==path||path.length()==0){
			path = "D:/screen/screen.png";
		}
		
		File file = new File(path);
		File dir = file.getParentFile();
		if(!dir.exists()){
			dir.mkdirs();
		}
		
	}
	
	public static void screenAndPull(){
		screenAndPull("");
	}

	public static void screenAndPull(String path) {
		if(null==path||path.length()==0){
			path = "D:/screen/screen.png";
		}
		
		File file = new File(path);
		File dir = file.getParentFile();
		if(!dir.exists()){
			dir.mkdirs();
		}
		
		CmdUtil.exec("adb shell /system/bin/screencap -p /sdcard/screenshot.png");
		sleep(100);
		CmdUtil.exec("adb pull /sdcard/screenshot.png " + path);
	}

	public static void sleep(long time) {
		try {
			Thread.sleep(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
